﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Urna_TP.Models;
using System.Data.Entity;

namespace Urna_TP.Controllers
{
    public class HomeController : Controller
    {
        UrnaPescarEntities2 db = new UrnaPescarEntities2();
        [HttpGet]
        public ActionResult Index()
        {
            var Candidatos = db.Candidato.ToList();
            return View(model: Candidatos);
        }
        [HttpPost]
        public ActionResult votar(int DNI, int candID)
        {
            Candidato c = new Candidato();
            Urna urna = new Urna();
            Votante v = null;
            using (var db = new UrnaPescarEntities2())
            {
                v = (from votante in db.Votante
                     where votante.DNI == DNI
                     select votante).FirstOrDefault();
                c = (from Postulante in db.Candidato
                     where Postulante.ID == candID
                     select Postulante).FirstOrDefault();
            }
            if (v != null)
            {
                if (!v.Voto)
                {
                    c.CantVotos++;                  
                    urna.Fecha = DateTime.Now;
                    urna.CandidtoID = candID;
                    v.Voto = true;
                    db.Urna.Add(urna);
                    db.Entry(v).State = System.Data.EntityState.Modified;
                    db.Entry(c).State = System.Data.EntityState.Modified;
                    db.SaveChanges();
                    return View("VotoSi");
                }
                else
                {
                    return View("VotoChanta");
                }
            }
            else
            {
                return View("VotoNo");
            }

            return RedirectToAction("Index");
        }



        public ActionResult Report()
        {
            var model = db.Candidato.ToList();

            return PartialView("_Report", model);
        }


        //public ActionResult About() 
        //{ 

        // //var nuevoCandidato = new Models.Candidato(); 
        // //nuevoCandidato.Nombre = "gise"; 
        // //context.Candidatos.Add(nuevoCandidato); 
        // //context.SaveChanges(); 



        // ViewBag.Message = "Your app description page."; 

        // return View(); 
        //} 

        //public ActionResult Contact() 
        //{ 
        // ViewBag.Message = "Your contact page."; 

        // return View(); 
        //} 
    }
}

